package com.ruoyi.dispatch.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dispatch.domain.MonthWashReport;
import com.ruoyi.dispatch.domain.WashDayReport;
import com.ruoyi.dispatch.enums.WashDayReportCol;
import com.ruoyi.dispatch.mapper.WashDayReportMapper;
import com.ruoyi.dispatch.service.IWashDayReportService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 洗煤日报Service业务层处理
 * 
 * @author wuxiaofang
 * @date 2025-04-11
 */
@Service
public class WashDayReportServiceImpl implements IWashDayReportService 
{
    @Autowired
    private WashDayReportMapper washDayReportMapper;

    /**
     * 查询洗煤日报
     * 
     * @param reportId 洗煤日报主键
     * @return 洗煤日报
     */
    @Override
    public WashDayReport selectWashDayReportByReportId(Long reportId)
    {
        return washDayReportMapper.selectWashDayReportByReportId(reportId);
    }

    /**
     * 查询洗煤日报列表
     * 
     * @param washDayReport 洗煤日报
     * @return 洗煤日报
     */
    @Override
    public List<WashDayReport> selectWashDayReportList(WashDayReport washDayReport)
    {
        return washDayReportMapper.selectWashDayReportList(washDayReport);
    }

    /**
     * 新增洗煤日报
     * 
     * @param washDayReport 洗煤日报
     * @return 结果
     */
    @Override
    public int insertWashDayReport(WashDayReport washDayReport)
    {
        washDayReport.setCreateTime(DateUtils.getNowDate());
        return washDayReportMapper.insertWashDayReport(washDayReport);
    }

    /**
     * 修改洗煤日报
     * 
     * @param washDayReport 洗煤日报
     * @return 结果
     */
    @Override
    public int updateWashDayReport(WashDayReport washDayReport)
    {
        washDayReport.setUpdateTime(DateUtils.getNowDate());
        return washDayReportMapper.updateWashDayReport(washDayReport);
    }

    /**
     * 批量删除洗煤日报
     * 
     * @param reportIds 需要删除的洗煤日报主键
     * @return 结果
     */
    @Override
    public int deleteWashDayReportByReportIds(Long[] reportIds)
    {
        return washDayReportMapper.deleteWashDayReportByReportIds(reportIds);
    }

    /**
     * 删除洗煤日报信息
     * 
     * @param reportId 洗煤日报主键
     * @return 结果
     */
    @Override
    public int deleteWashDayReportByReportId(Long reportId)
    {
        return washDayReportMapper.deleteWashDayReportByReportId(reportId);
    }

    @Override
    public List<MonthWashReport> countWashReport(WashDayReport washDayReport) {
        SimpleDateFormat sourceFormatMonth = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sourceFormatYear = new SimpleDateFormat("yyyy");
        String month = sourceFormatMonth.format(washDayReport.getStatisticsDate());
        String year = sourceFormatYear.format(washDayReport.getStatisticsDate());
        List<WashDayReport> dayReports = washDayReportMapper.selectWashDayReportList(washDayReport);
        List<MonthWashReport> mothList = washDayReportMapper.monthWashReport(month);
        List<MonthWashReport> yearList = washDayReportMapper.yearWashReport(year);
        for (WashDayReport dayReport : dayReports) {
            for (MonthWashReport monthWashReport : mothList) {
                if (dayReport.getCoalId().equals(monthWashReport.getCoalId())){
                    BeanUtils.copyProperties( dayReport,monthWashReport);
                }
                for (MonthWashReport yearReport : yearList) {
                    if (monthWashReport.getCoalId().equals(yearReport.getCoalId())){
                        monthWashReport.setYearClean(yearReport.getYearClean());
                    }
                }
            }
        }
        return mothList;
    }

    @Override
    public List<Map<String, String>> handleColumnMap(String[] columnArray) {
        List<Map<String, String>> columns = Lists.newArrayList();
        for (String item : columnArray) {
            Map column = WashDayReportCol.getByCode(item);
            if (column != null) {
                columns.add(column);
            }
        }
        return columns;
    }


}
